/*
 * @Author: Jason Liu
 * @Date: 2021-03-09 13:39:48
 * @Desc: 脚手架初始化
 */
const CopyWebpackPlugin = require("copy-webpack-plugin");
const webpack = require("webpack");
const path = require("path");
const package = require("./package.json");
const StatsPlugin = require("stats-webpack-plugin");

const CompressionPlugin = require("compression-webpack-plugin");
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;

function resolve(dir) {
  return path.join(__dirname, dir);
}
// process.env.VUE_APP_BASE_URL = process.env.NODE_ENV === 'development' ? "/winningmodel" : "/winningmodel";
require("events").EventEmitter.defaultMaxListeners = 0;

/*
    环境信息
        UAT环境：http://172.17.1.96         liu.chao Password01!
        FAT环境：http://172.16.0.187        admin    Winning@1109 
        FAT环境：http://172.17.1.80         liu.chao Password01!    http://172.16.0.187:8888/datafactory/login
                172.17.1.80     root    bF^95wAuvV
        测试环境：http://172.17.0.56         winningAdmin Winning@123

    wxp部署环境
        http://172.16.7.77:8089/cluster/cluster/portal/webclient
        前端运维 ——> 分组:60DF测试环境_DEV   

*/
const main_ip = "http://172.17.1.96"; //"";//主应用IP //http://172.16.0.187;  //
// const main_ip ="http://172.17.0.56";//"";//主应用IP //http://172.16.0.187;  //
// const main_ip ="http://172.16.0.187";

// vue.config.js
const vueConfig = {
  publicPath: process.env.VUE_APP_BASE_URL,
  // pages: platform.appPage,
  configureWebpack: {
    performance: { hints: false },
    plugins: [
      // Ignore all locale files of moment.js
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
      // 复制自定义的静态资源
      new CopyWebpackPlugin([
        {
          from: path.resolve(__dirname, "./static"),
          to: "./static",
          ignore: [".*"]
        }
      ]),
      new webpack.ProvidePlugin({}),
      new StatsPlugin("manifest.json", {
        chunkModules: true,
        entrypoints: true,
        source: true,
        chunks: true,
        modules: true,
        assets: true,
        children: true,
        exclude: [/node_modules/]
      })
    ],
    // 导出umd格式的包，在全局对象上挂载属性package.name，基座应用需要通过这个全局对象获取一些信息，比如子应用导出的生命周期函数
    output: {
      library: `${package.name}-[name]`,
      libraryTarget: "umd",
      jsonpFunction: `webpackJsonp_${package.name}`
    }
  },
  chainWebpack: config => {
    config.resolve.alias.set("w-vue-chongqing", resolve("./"));

    // svg rule loader
    const svgRule = config.module.rule("svg"); // 找到svg-loader
    svgRule.uses.clear(); // 清除已有的loader, 如果不这样做会添加在此loader之后
    svgRule.exclude
      .add(/node_modules/)
      .add("/node_modules/w-vue-middle/assets/svg"); // 正则匹配排除node_modules目录
    svgRule // 添加svg新的loader处理
      .test(/\.svg$/)
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]"
      });

    // 修改images loader 添加svg处理
    const imagesRule = config.module.rule("images");
    imagesRule.exclude.add(resolve("src/assets/svg"));
    config.module.rule("images").test(/\.(png|jpe?g|gif|svg)(\?.*)?$/);
    if (process.env.NODE_ENV === "production") {
      config
        .plugin("compression-webpack-plugin")
        .use(
          new CompressionPlugin({
            filename: "[path].gz[query]",
            algorithm: "gzip",
            test: productionGzipExtensions,
            threshold: 10240, //对超过10k的数据进行压缩
            minRatio: 0.8,
            deleteOriginalAssets: process.env.NODE_ENV === "development" //删除源文件
          })
        )
        .end();
    }
  },
  css: {
    loaderOptions: {
      less: {
        javascriptEnabled: true
      }
    }
  },
  devServer: {
    // development server port 8000
    port: 8085,
    open: true,
    // If you want to turn on the proxy
    // mock service添加的服务,通过run_service.bat启动，启动前注意安全相关依赖包
    proxy: {
      "/portalService": {
        //统一登录服务
        //target: "http://172.17.1.205:8500/",
        //target: "http://1.15.45.84:8500/",
        // target: "http://172.16.0.187:51980/api/v1/",
        //target: "http://172.17.0.183:51980/api/v1/",
        target: main_ip + ":51980/api/v1/",
        changeOrigin: true,
        pathRewrite: {
          "^/portalService": ""
        }
      },
      "/userService": {
        //用户服务
        //target: "http://172.16.9.97:8190/",  proxy_pass http://172.17.0.183:51980/;
        // target: "http://172.16.0.187:51980/",
        //target: "http://172.17.0.183:51980/",
        target: main_ip + ":51980/",
        changeOrigin: true,
        pathRewrite: {
          "^/userService": ""
        }
      },
      "/profile": {
        target: "http://172.16.0.187:8888/",
        changeOrigin: true
      },
      "/dataModelService": {
        // target: main_ip + ":8888/",
        target: main_ip + ":8085/",
        changeOrigin: true,
        pathRewrite: {
          "^/dataModelService": ""
        }
      },
      "/metadataService": {
        //元数据
        target: main_ip + ":8888/",
        changeOrigin: true
        // pathRewrite: {
        //   "^/metadataService": ""
        // }
      },
      "/dataIntegration": {
        //数据集成
        // target: main_ip + ":8888/",
        target: "http://172.17.1.97:8190/",
        // target: "http://172.17.17.13:8190/",
        changeOrigin: true,
        pathRewrite: {
          "^/dataIntegration": ""
        }
      },
      "/dispatchCenterService": {
        //调度中心
        target: main_ip + ":51960/",
        changeOrigin: true,
        pathRewrite: {
          "^/dispatchCenterService": ""
        }
      },
      "/webui-dataview": {
        target: "http://172.16.110.138:8788/",
        changeOrigin: true
      }
    },
    headers: {
      "Access-Control-Allow-Origin": "*"
    }
  },
  // disable source map in production，是否源代码压缩映射map文件
  productionSourceMap: process.env.NODE_ENV === "development",
  lintOnSave: false,
  outputDir: "dist",
  // babel-loader no-ignore node_modules/*
  transpileDependencies: ["monaco-editor"],
  // Solution For Issue:You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.
  // zhengkai.blog.csdn.net
  runtimeCompiler: true,
  filenameHashing: true
};

module.exports = {
  ...vueConfig
};
